package org.grow.sha;


import org.grow.sha.client.ButterFly;
import org.grow.sha.tool.RobotEx;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.awt.*;

@SpringBootApplication
public class ShaApp {
    public static Logger logger = LoggerFactory.getLogger(ShaApp.class);
    public static void main(String[] args) throws InterruptedException {
        System.setProperty("java.awt.headless","false");
        ConfigurableApplicationContext run = SpringApplication.run(ShaApp.class, args);

        ThreadPoolTaskExecutor es = run.getBean(ThreadPoolTaskExecutor.class);
        es.execute(run.getBean(ButterFly.class));
        Thread.sleep(3000);
        es.shutdown();
        logger.info("主线程结束了");


    }
    //    @Bean
//    public ThreadPoolTaskScheduler taskScheduler(){
//        ThreadPoolTaskExecutor scheduler = new ThreadPoolTaskExecutor();
//        scheduler.setPoolSize(4);
//        scheduler.setThreadNamePrefix("task-");  //设置线程名开头
//        scheduler.setAwaitTerminationSeconds(60);
//        scheduler.setWaitForTasksToCompleteOnShutdown(true);
//        return scheduler;
//    }
    @Bean(name = {"taskExecutor","applicationTaskExecutor","es"})
    public ThreadPoolTaskExecutor taskExecutor(){
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setCorePoolSize(2);
        taskExecutor.setMaxPoolSize(4);
        taskExecutor.setQueueCapacity(10);
        taskExecutor.setThreadNamePrefix("es-");
        taskExecutor.setWaitForTasksToCompleteOnShutdown(true);
        taskExecutor.initialize();
        return taskExecutor;
    }
    @Bean(name = {"robot","robotEx"})
    public RobotEx robot(){
        try {
            return new RobotEx();
        } catch (AWTException e) {
            e.printStackTrace();
        }
        return null;
    }
}
